/* eslint-disable vue/no-use-v-if-with-v-for */
<!-- xuwencheng  2022年6年29日18:00  这是我的wenzhangbianji -->
<!-- 编辑 -->
<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destory-on-close="true"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="65%"
  >
    <el-form ref="form" label-width="200px" :model="form" :rules="rules">
      <el-form-item label="职位内容" prop="jobInfo">
        <el-input v-model="form.jobInfo" placeholder="请输入职位内容" />
      </el-form-item>
      <el-form-item label="职位标题" prop="jobHead">
        <el-input v-model="form.jobHead" placeholder="请输入职位标题" />
      </el-form-item>
      <el-form-item label="所属角色" prop="actorClassifyIds">
        <el-select
          v-model="form.actorClassifyIds"
          multiple
          placeholder="请选择所属角色"
        >
          <el-option
            v-for="item in options2"
            :key="item.id"
            :label="item.classifyName"
            :value="item.id"
            @click.native="chooseone(item)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" prop="linkmans">
        <el-input v-model="form.linkmans" placeholder="请输入导联系人" />
      </el-form-item>
      <el-form-item label="联系人手机号" prop="linkphone">
        <el-input v-model="form.linkphone" placeholder="请输入联系人手机号" />
      </el-form-item>
      <el-form-item label="联系人微信号" prop="linkWxnum">
        <el-input v-model="form.linkWxnum" placeholder="请输入联系人微信号" />
      </el-form-item>
      <el-form-item label="城市" prop="jobCityId">
        <div class="block">
          <el-cascader
            ref="cascader"
            v-model="form.jobCityId"
            filterable
            :options="optionswo"
            placeholder="试试搜索：指南"
            @change="cascar"
          />
        </div>
        当前选择： {{ form.crwCityName }}
      </el-form-item>
      <el-form-item label="是否选择剧组" prop="crewNeed">
        <el-radio v-model="form.crewNeed" label="1">选择</el-radio>
        <el-radio v-model="form.crewNeed" label="0">不选择</el-radio>
      </el-form-item>
      <el-form-item label="查询剧组">
        <el-input
          v-model="keyWord"
          clearable
          placeholder="请输入查询的剧组"
          style="width: 30%"
        />
        <el-button
          icon="el-icon-search"
          style="margin-left: 10px"
          type="primary"
          @click="queryDatass"
        >
          查询
        </el-button>
      </el-form-item>
      <el-form-item v-if="options1 != ''" label="查询结果">
        <el-radio-group>
          <el-radio
            v-for="item in options1"
            :key="item.id"
            :label="item.crewName"
            :value="item.id"
            @click.native="enterprise(item)"
          >
            {{ item.crewName }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-else label="查询结果">
        {{ shujv }}
      </el-form-item>
      <!-- prop="crewId" -->
      <el-form-item label="选择结果">
        <span>{{ neirong }}</span>
      </el-form-item>
      <el-form-item label="所属区域" prop="jobAreaId">
        <el-select v-model="form.jobAreaId" placeholder="请选择所属区域">
          <el-option
            v-for="item in options7"
            :key="item.id"
            :label="item.areaName"
            :value="item.id"
            @click.native="choosetwo(item)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否指定领队" prop="needTeamleader">
        <el-radio v-model="form.needTeamleader" label="1">指定</el-radio>
        <el-radio v-model="form.needTeamleader" label="0">不指定</el-radio>
      </el-form-item>
      <el-form-item
        v-if="form.needTeamleader == 1"
        label="查询领队"
        prop="teamLeaderIds"
      >
        <el-input
          v-model="keyWordtwo"
          clearable
          placeholder="请输入查询的领队"
          style="width: 30%"
        />
        <el-button
          icon="el-icon-search"
          style="margin-left: 10px"
          type="primary"
          @click="queryDatatwo"
        >
          查询
        </el-button>
      </el-form-item>
      <el-form-item
        v-if="options3 != '' && form.needTeamleader == 1"
        label="查询结果"
      >
        <el-checkbox-group
          v-model="form.teamLeaderIds"
          @change="handleCheckedCitiesChange"
        >
          <el-checkbox v-for="item in options3" :key="item.id" :label="item.id">
            {{ item.leaNickName }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item
        v-else-if="options3 == '' && form.needTeamleader == 1"
        label="查询选择结果"
      >
        <span v-if="options3 == ''">
          {{ shujvtwo }}
        </span>
      </el-form-item>
      <!-- <el-form-item v-if="form.needTeamleader == 1" label="选择结果">
        <span>{{ neirongtwo }}</span>
      </el-form-item> -->
      <el-form-item label="定位信息" prop="jobAddress">
        <el-input v-model="form.jobAddress" placeholder="请输入定位信息" />
      </el-form-item>

      <el-form-item label="人数" prop="peopleNum">
        <el-input v-model="form.peopleNum" placeholder="请输入人数" />
      </el-form-item>
      <el-form-item label="所需年龄区间" prop="ageId">
        <el-select v-model="form.ageId" placeholder="请选择所需年龄区间">
          <el-option
            v-for="item in options9"
            :key="item.id"
            :label="item.ageRange"
            :value="item.id"
            @click.native="chooseonetwo(item)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否需要男性演员" prop="crwMale">
        <el-radio v-model="form.crwMale" label="1">需要</el-radio>
        <el-radio v-model="form.crwMale" label="0">不需要</el-radio>
      </el-form-item>
      <el-form-item label="是否需要女性演员" prop="crwFemale">
        <el-radio v-model="form.crwFemale" label="1">需要</el-radio>
        <el-radio v-model="form.crwFemale" label="0">不需要</el-radio>
      </el-form-item>
      <el-form-item label="截至日期时间" prop="deadline2">
        <el-date-picker
          v-model="form.deadline2"
          format="yyyy 年 MM 月 dd 日"
          placeholder="选择日期"
          type="date"
          value-format="timestamp"
        />
      </el-form-item>

      <el-form-item label="排序" prop="jobOrder">
        <el-input v-model="form.jobOrder" />
      </el-form-item>
      <el-form-item label="导演名称" prop="director">
        <el-input v-model="form.director" placeholder="请输入导演名称" />
      </el-form-item>
      <el-form-item label="规格" prop="director">
        <table>
          <thead>
            <tr>
              <td v-for="(item, index) in disHeadList" :key="index">
                {{ item }}
              </td>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item, index) in disBodyList" :key="index">
              <td v-for="(itemX, indexX) in item" :key="indexX">
                {{ itemX }}
              </td>
            </tr>
          </tbody>
        </table>
      </el-form-item>
    </el-form>
    <el-button class="baocun" size="medium" type="primary" @click="save">
      保存
    </el-button>
  </el-dialog>
</template>

<script>
  /* eslint-disable*/
  import {
    doAdd,
    serach,
    starareacrewjob,
    serachtwo,
    agerange,
    doEdit,
  } from '@/api/userManagement'
  import { roleId } from '@/api/yanyuan'
  import { stringify } from 'querystring'
  import { city } from '../guanli/js/city'

  //import { number } from 'echarts'
  export default {
    name: 'search',

    data() {
      // 手机号码验证
      var contactPhone = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('手机号不能为空'))
        } else {
          const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
          if (reg.test(value)) {
            callback()
          } else {
            return callback(new Error('请输入正确的手机号'))
          }
        }
      }
      const validatesurfacePlot = (rule, value, callback) => {
        if (this.form.actorPicture === '') {
          callback(new Error('请添加头像图片!!!!'))
        } else {
          callback()
        }
      }
      const validatesurfacePlotwo = (rule, value, callback) => {
        if (this.form.moka === '') {
          callback(new Error('请添加摩卡图片!!!!'))
        } else {
          callback()
        }
      }
      return {
        disBodyList: [],
        disHeadList: [],

        checked: true,
        optionswo: city,
        selectedOptions: [],
        keyWord: '', //关键词
        keyWordtwo: '', //领队关键词
        token: '',
        imageUrl: '',
        neirong: '', //查询剧组内容
        neirongtwo: [], //查询领队内容
        imageUrltwo: '', //摩卡
        value: true,
        value1: true,
        dialogVisible: false,
        disabled: false,
        options1: [],
        options2: [],
        options3: [],
        options7: [], //选择区域
        options9: [], //选择年龄
        valueone: '', //查询
        valuetwo: '',
        shujv: '暂无数据',
        shujvtwo: '暂无数据', //查询领队
        form: {
          actorClassifyIds: [],
          crwCityName: '', //城市名称
          crewNeed: '1', //是否选择领队
          crwAreaName: '', //区域名称不能为空,
          teamLeaderIds: [], //指定领队的情况下，领队的id
          needTeamleader: '0', //是否指定领队0-不指定 1-指定
          // deadline: '', //截至日期时间戳不能为空
          crwMale: '1', //是否需要男性演员 0-否 1-是
          crwFemale: '1', //是否需要女性演员 0-否 1-是
          jobCityId: '', //城市id
        },
        jiezhial: '', //截止日期
        activated() {
          if (this.$refs.MaskVideo.src) this.$refs.MaskVideo.play()
        },
        deactivated() {
          if (this.$refs.MaskVideo.src) this.$refs.MaskVideo.pause()
        },

        rules: {
          jobInfo: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入职位内容',
            },
          ],
          jobHead: [
            {
              required: true,
              trigger: 'blur',
              message: '职位标题不能为空',
            },
          ],
          actorClassifyIds: [
            {
              required: true,
              trigger: 'change',
              message: '请选择职位所需角色',
            },
          ],
          crewNeed: [
            {
              required: true,
              trigger: 'change',
              message: '是否选择领队',
            },
          ],
          linkphone: [
            {
              required: true,
              trigger: 'blur',
              validator: contactPhone,
            },
          ],
          // emergencyContPhone: [
          //   {
          //     required: true,
          //     trigger: 'blur',
          //     validator: contactPhone,
          //   },
          // ],
          linkmans: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入导演名称',
            },
          ],
          linkWxnum: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入联系人微信号',
            },
          ],
          crewId: [
            {
              required: true,
              trigger: 'change',
              message: '请选择剧组',
            },
          ],
          jobAddress: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入定位信息',
            },
          ],

          jobCityId: [
            {
              required: true,
              trigger: 'change',
              message: '请选择城市',
            },
          ],
          leaNickName: [
            {
              required: true,
              trigger: 'change',
              message: '请输入领队名称',
            },
          ],
          jobAreaId: [
            {
              required: true,
              trigger: 'change',
              message: '请选择区域',
            },
          ],
          moka: [
            {
              required: true,
              trigger: 'change',
              message: '请添加图片',
            },
            {
              validator: validatesurfacePlotwo,
              trigger: 'change',
            },
          ],
          jobAddress: [
            {
              required: true,
              message: '定位信息不能为空',
              trigger: 'blur',
            },
          ],
          leaderIntro: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入领队简介',
            },
          ],
          // enterprise: [
          //   {
          //     required: true,
          //     trigger: 'change',
          //     message: '请选择领队/经纪公司',
          //   },
          // ],
          areaId: [
            {
              required: true,
              trigger: 'change',
              message: '请选择区域',
            },
          ],
          roleId: [
            {
              required: true,
              trigger: 'change',
              message: '请选择所属角色',
            },
          ],
          peopleNum: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入人数',
            },
            {
              pattern: /^(\d+)((?:\.\d+)?)$/,
              message: '请输入合法数字',
            },
          ],
          jobOrder: [
            {
              pattern: /^(\d+)((?:\.\d+)?)$/,
              message: '请输入合法数字',
            },
          ],
          wxNumber: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入微信号',
            },
          ],
          ageId: [
            {
              required: true,
              trigger: 'change',
              message: '所需年龄区间不能为空',
            },
          ],
          deadline: [
            {
              required: true,
              trigger: 'change',
              message: '截至日期不能为空',
            },
          ],
          director: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入导演名称',
            },
          ],
          freeTime: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入空闲时间',
            },
          ],
          inWork: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入参演作品',
            },
          ],
          other: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入其他',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.token = this.$store.getters['user/token']
      this.fetchDatatwo()
      this.fetchDatathree()
      this.fetchDatafour()
    },
    methods: {
      enterprise(e) {
        this.form.crewId = e.id

        this.neirong = e.crewName
      },
      enterprisetwo(e) {
        this.form.teamLeaderIds = e.id
        console.log(this.form.teamLeaderIds)
      },

      cascar(e) {
        //选择城市
        this.form.crwCityName =
          // this.$refs['cascader'].getCheckedNodes()[0].pathLabels[0] +
          // '--' +
          // this.$refs['cascader'].getCheckedNodes()[0].pathLabels[1] +
          // '--' +
          this.$refs['cascader'].getCheckedNodes()[0].pathLabels[2]

        this.form.jobCityId = e[2]
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return true
      },
      beforeAvatarUploadtwo(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return true
      },
      choose(e) {
        this.form.crewTheme = e.themeName
        this.form.crewThemeId = e.id
      },
      chooseone(e) {}, //所属角色
      choosetwo(e) {
        //选择区域

        this.form.crwAreaName = e.areaName
      },
      chooseonetwo(e) {
        //选择年龄区间
        console.log(e)
        this.form.ageId = e.id
        this.form.needAge = e.ageRange
      },
      handleChange(a) {
        this.form.particulars = a
      },
      handleAvatarSuccess(res, file, fileList) {
        this.imageUrl = URL.createObjectURL(file.raw)
        console.log(res)
        this.form.actorPicture = res.data
      },
      handleAvatarSuccesstwo(res, file, fileList) {
        this.imageUrltwo = URL.createObjectURL(file.raw)
        this.form.moka = res.data
      },
      calcDescartes(array) {
        if (array.length < 2) return array[0] || []
        return array.reduce((total, currentValue) => {
          let res = []

          total.forEach((t) => {
            currentValue.forEach((cv) => {
              if (t instanceof Array)
                // 或者使用 Array.isArray(t)
                res.push([...t, cv])
              else res.push([t, cv])
            })
          })
          return res
        })
      },
      showEdit(row) {
        console.log(row.crwFemale)
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.form.crewNeed = String(row.crewNeed)
          this.form.needTeamleader = String(row.needTeamleader)
          this.form.crwMale = String(row.crwMale)
          this.form.crwFemale = String(row.crwFemale)
          // console.log(this.form.crwMale, this.form.crwFemale)
          this.neirong = row.crewName
          this.jiezhial = row.deadline
          console.log(row)

          this.form.jobDetailJson = JSON.parse(this.form.jobDetailJson)
          var nullList = []
          this.form.jobDetailJson.map((x) => {
            nullList.push(x.values)
            this.disHeadList.push(x.name)
          })
          var disList = this.calcDescartes(nullList)
          disList.map((x, index) => {
            console.log(Array.isArray(x))
            if (!Array.isArray(x)) {
              disList[index] = [x, this.form.needNums[index]]
            } else {
              x[x.length] = this.form.needNums[index]
            }
          })
          disList = disList.filter((x) => {
            if (x[x.length - 1] > 0) {
              return true
            }
          })
          this.disBodyList = disList
          ///////////////////////////////////////////////
          console.log('nullListnullListnullListnullList')
          console.log(this.disHeadList)
          console.log(this.disBodyList)
          console.log('nullListnullListnullListnullList')
          ///////////////////////////////////////////////
        }
        this.dialogFormVisible = true
      },
      close() {
        let userId = this.form.userId
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        if (!this.form.id) {
          this.form.userId = userId
        }

        this.dialogFormVisible = false
        this.imageUrl = ''
        this.imageUrltwo = ''
        // this.form = ''
      },
      sethealth(e) {
        this.form.health = e
      },
      sethealthtwo(e) {
        this.form.workType = e
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              this.form.deadline2 =
                this.form.deadline2 + 24 * 60 * 60 * 1000 - 1
              this.form.deadline = this.form.deadline2
              const msg = doEdit(this.form)

              msg.then((res) => {
                console.log(res.msg)
                if (res.msg === '更新成功') {
                  this.$baseMessage(
                    res.msg,
                    'success',
                    'vab-hey-message-success'
                  )
                  this.$emit('fetch-data')
                  this.close()
                }
              })
            } else {
              this.form.deadline2 =
                this.form.deadline2 + 24 * 60 * 60 * 1000 - 1
              this.form.deadline = this.form.deadline2
              const msg = doAdd(this.form)
              this.$emit('fetch-data')
              this.close()
              msg.then((res) => {
                console.log(res.msg)
                if (res.msg === '添加成功') {
                  this.$baseMessage(
                    res.msg,
                    'success',
                    'vab-hey-message-success'
                  )
                  this.$router.push({
                    path: '/userManagement/shixianglist',
                  })
                }
              })
            }
          }
        })
      },
      nickNameChange() {
        this.$forceUpdate()
      },
      queryDatass() {
        this.queryData()
      },
      queryData() {
        //chaxun
        this.disabled = true
        let keyWord = this.keyWord
        const msg = serach({
          keyWord: keyWord,
        })
        msg.then((res) => {
          if (res.msg == 'success') {
            this.options1 = res.data
          }
        })
      },
      queryDatatwo() {
        //查询领队
        let keyWord = this.keyWordtwo
        const msg = serachtwo({
          keyWord: keyWord,
        })

        msg.then((res) => {
          if (res.msg == 'success') {
            this.options3 = res.data
          }
        })
      }, //查询领队
      async fetchDatatwo() {
        let queryForm = {}
        const res = await roleId(queryForm)
        this.options2 = res.data
        console.log(res)
      },
      async fetchDatathree() {
        //选择区域
        let queryForm = {}
        const res = await starareacrewjob(queryForm)
        this.options7 = res.data
        console.log(res)
      },
      async fetchDatafour() {
        //选择年0
        let queryForm = {}
        const res = await agerange(queryForm)
        this.options9 = res.data
        console.log(res)
      },
      handleCheckedCitiesChange(value) {
        console.log(value)
        console.log(this.form.teamLeaderIds)
        // this.neirongtwo = value
        let checkedCount = value.length
        // this.checkAll = checkedCount === this.cities.length
        // this.isIndeterminate =
        //   checkedCount > 0 && checkedCount < this.cities.length
      },
    },
  }
</script>
<style>
  /* .huakuai {
    position: relative;
    left: 96%;
  } */
  .avatar-uploader .el-upload {
    position: relative;
    overflow: hidden;
    cursor: pointer;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    width: 178px;
    height: 178px;
    font-size: 28px;
    line-height: 178px;
    color: #8c939d;
    text-align: center;
  }

  .avatar {
    display: block;
    width: 178px;
    height: 178px;
  }

  .baocun {
    width: 100%;
    height: 100%;
  }
</style>
